package com.crm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crm.common.ResVo;
import com.crm.entity.News;
import com.crm.service.NewsService;
import com.spring4all.swagger.EnableSwagger2Doc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@EnableSwagger2Doc
@RequestMapping("/news")
@Api(tags = "新闻管理")
public class NewsController {

	@Autowired
	private NewsService newsService;

	@GetMapping("/getNews")
	@ApiOperation("获取新闻列表")
	public ResVo<Object> findNews() {
		List<News> message = newsService.findNews();
		return ResVo.success(message);
	}

	@GetMapping("/getNewsByPage/{pageIndex}")
	@ApiOperation("获取新闻列表带分页")
	public ResVo<Object> findNewsByPage(@RequestParam(value="title",required=false) @ApiParam("新闻标题") String title,
			@PathVariable("pageIndex") @ApiParam("页码") Integer pageIndex) {
		Page<News> message = newsService.findNewsByPage(pageIndex, title);
		return ResVo.success(message);
	}

	@PostMapping("/addNews")
	@ApiOperation("添加新闻")
	public ResVo<Object> addNews(@RequestBody News news) {
		newsService.addNews(news);
		return ResVo.defaultSuccess();
	}

	@GetMapping("/getNews/{id}")
	@ApiOperation("按新闻id查询新闻")
	public ResVo<Object> findNewsByPage(@PathVariable @ApiParam("新闻id") String id) {
		News message = newsService.findNewsById(id);
		return ResVo.success(message);
	}

}
